﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccess.Models;

namespace DataAccess.Repository
{
	public class GroupRepository : IGroupRepository, IDisposable
	{
		private KPIDbContext _KPIDbContext = new KPIDbContext();

		#region IDisposable Members

		public void Dispose()
		{
			if(_KPIDbContext != null)
			{
				_KPIDbContext.Dispose();
			}
		}

		#endregion

		#region IGroupRepository Members

		/// <summary>
		/// 获取所有单位类别
		/// </summary>
		/// <returns></returns>
		public List<Models.Group> GetAllGroups()
		{
			List<Group> result = null;

			try
			{
				var groups = from g in _KPIDbContext.Group
										 join gc in _KPIDbContext.GroupCategory on g.GroupCategoryID equals gc.GroupCategoryID into g2
										 from ggc in g2.DefaultIfEmpty()
										 orderby g.DisplayOrder
										 select new
										 {
											 GroupCategoryName = ggc.GroupCategoryName,
											 GroupName = g.GroupName,
											 GroupID = g.GroupID,
											 GroupAddTime = g.GroupAddTime,
											 GroupCategoryID = g.GroupCategoryID,
											 GroupDescription = g.GroupDescription,
											 GroupModifyTime = g.GroupModifyTime,
											 GroupDispName = g.GroupDispName,
											 GroupShortName = g.GroupShortName
										 };

				if(groups != null)
				{
					var lstGroups = groups.ToList();

					if(lstGroups != null && lstGroups.Count > 0)
					{
						result = new List<Group>();

						foreach(var g in lstGroups)
						{
							result.Add(new Group()
							{
								GroupID = g.GroupID,
								GroupModifyTime = g.GroupModifyTime,
								GroupDescription = g.GroupDescription,
								GroupCategoryID = g.GroupCategoryID,
								GroupAddTime = g.GroupAddTime,
								GroupName = g.GroupName,
								GroupDispName = g.GroupDispName,
								GroupShortName = g.GroupShortName,
								TheGroupCategory = new GroupCategory()
								{
									GroupCategoryID = g.GroupCategoryID,
									GroupCategoryName = g.GroupCategoryName
								}
							});
						}
					}
				}
			}
			catch
			{
				throw;
			}

			return result;
		}

		/// <summary>
		/// 取指定ID的单位
		/// </summary>
		/// <param name="groupID"></param>
		/// <returns></returns>
		public Group GetTheGroup(int groupID)
		{
			Group result = null;

			if(groupID > 0)
			{
				try
				{
					//result = _KPIDbContext.Group.Include("ListKPIs.TheKPIProgress").FirstOrDefault(g => g.GroupID == groupID);
					var data = from kpi in _KPIDbContext.KPI
										 join kpip in _KPIDbContext.KPIProgress on kpi.KPIID equals kpip.KPIID into t1
										 from kpikpip in t1.DefaultIfEmpty()
										 join g in _KPIDbContext.Group on kpi.GroupID equals g.GroupID into t2
										 from kpig in t2.DefaultIfEmpty()
										 where kpig.GroupID == groupID && kpi.CurrentState != KPIStates.Delete && kpi.TheYear == DateTime.Now.Year
										 select new
										 {
											 kpig.GroupID,
											 kpig.GroupName,
											 kpig.GroupShortName,
											 kpig.GroupDispName,
											 kpig.DisplayOrder,
											 kpig.GroupAddTime,
											 kpig.GroupCategoryID,
											 kpig.GroupDescription,
											 kpig.GroupModifyTime,
											 kpig.ShowOnHome,
											 kpi.ActionPlans,
											 kpi.CreateUserName,
											 kpi.CurrentState,
											 kpi.FinishTime,
											 kpi.KPIAddTime,
											 kpi.KPIContent,
											 kpi.KPIID,
											 kpi.KPIModifyTime,
											 kpi.PreState,
											 kpi.ResponsablePerson,
											 kpi.ResponseDepartment,
											 kpi.ResponsibleLeadership,
											 kpi.RLTel,
											 kpi.RPTel,
											 kpi.StartTime,
											 kpi.TheYear,
											 kpikpip.EndOfTheYearAssessment,
											 kpikpip.KPIProgressAddTime,
											 kpikpip.KPIProgressModifyTime,
											 kpikpip.KPIProgressRemark,
											 kpikpip.KPIProgressState,
											 kpikpip.Quarter1Progress,
											 kpikpip.Quarter2Progress,
											 kpikpip.Quarter3Progress,
											 kpikpip.Quarter4Progress
										 };

					if(data != null)
					{
						var lstData = data.ToList();

						if(lstData != null && lstData.Count > 0)
						{
							result = new Group();

							result.DisplayOrder = lstData[0].DisplayOrder;
							result.GroupAddTime = lstData[0].GroupAddTime;
							result.GroupCategoryID = lstData[0].GroupCategoryID;
							result.GroupDescription = lstData[0].GroupDescription;
							result.GroupDispName = lstData[0].GroupDispName;
							result.GroupID = lstData[0].GroupID;
							result.GroupModifyTime = lstData[0].GroupModifyTime;
							result.GroupName = lstData[0].GroupName;
							result.GroupShortName = lstData[0].GroupShortName;
							result.ShowOnHome = lstData[0].ShowOnHome;

							result.ListKPIs = new List<KPI>();

							foreach(var item in lstData)
							{
								var kpi = new KPI()
								{
									ActionPlans = item.ActionPlans,
									TheYear = item.TheYear,
									StartTime = item.StartTime,
									RPTel = item.RPTel,
									RLTel = item.RLTel,
									ResponsibleLeadership = item.ResponsibleLeadership,
									CreateUserName = item.CreateUserName,
									CurrentState = item.CurrentState,
									FinishTime = item.FinishTime,
									GroupID = item.GroupID,
									KPIAddTime = item.KPIAddTime,
									KPIContent = item.KPIContent,
									KPIID = item.KPIID,
									KPIModifyTime = item.KPIModifyTime,
									PreState = item.PreState,
									ResponsablePerson = item.ResponsablePerson,
									ResponseDepartment = item.ResponseDepartment
								};

								kpi.TheKPIProgress = new KPIProgress()
								{
									EndOfTheYearAssessment = item.EndOfTheYearAssessment,
									KPIID = item.KPIID,
									KPIProgressAddTime = item.KPIProgressAddTime,
									KPIProgressModifyTime = item.KPIProgressModifyTime,
									KPIProgressRemark = item.KPIProgressRemark,
									KPIProgressState = item.KPIProgressState,
									Quarter1Progress = item.Quarter1Progress,
									Quarter2Progress = item.Quarter2Progress,
									Quarter3Progress = item.Quarter3Progress,
									Quarter4Progress = item.Quarter4Progress
								};

								result.ListKPIs.Add(kpi);
							}
						}
					}
				}
				catch
				{
					throw;
				}
			}

			return result;
		}

		/// <summary>
		/// 获取所有单位类别及KPI
		/// </summary>
		/// <returns></returns>
		public List<Group> GetAllGroupWithKPIs(int y)
		{
			List<Group> result = null;

			if(y > 0)
			{
				try
				{
					var kpis = from kpi in _KPIDbContext.KPI
										 join g in _KPIDbContext.Group on kpi.GroupID equals g.GroupID into kpi2
										 from kpig in kpi2.DefaultIfEmpty()
										 join gc in _KPIDbContext.GroupCategory on kpig.GroupCategoryID equals gc.GroupCategoryID into g2
										 from ggc in g2.DefaultIfEmpty()
										 where kpi.TheYear == y
										 orderby kpig.DisplayOrder
										 select new
										 {
											 GroupCategoryName = ggc.GroupCategoryName,
											 GroupName = kpig.GroupName,
											 GroupID = kpig.GroupID,
											 GroupAddTime = kpig.GroupAddTime,
											 GroupCategoryID = kpig.GroupCategoryID,
											 GroupDescription = kpig.GroupDescription,
											 GroupModifyTime = kpig.GroupModifyTime,
											 GroupDispName = kpig.GroupDispName,
											 GroupShortName = kpig.GroupShortName,
											 KPIID = kpi.KPIID,
											 KPIContent = kpi.KPIContent,
											 KPIAddTime = kpi.KPIAddTime,
											 KPIModifyTime = kpi.KPIModifyTime,
											 StartTime = kpi.StartTime,
											 FinishTime = kpi.FinishTime,
											 ResponseDepartment = kpi.ResponseDepartment,
											 ResponsablePerson = kpi.ResponsablePerson,
											 RPTel = kpi.RPTel,
											 ResponsibleLeadership = kpi.ResponsibleLeadership,
											 RLTel = kpi.RLTel,
											 ActionPlans = kpi.ActionPlans,
											 CurrentState = kpi.CurrentState,
											 PreState = kpi.PreState,
											 TheYear = kpi.TheYear
										 };

					if(kpis != null)
					{
						var lstKPIs = kpis.ToList();

						if(lstKPIs != null && lstKPIs.Count > 0)
						{
							result = new List<Group>();

							foreach(var item in lstKPIs)
							{
								if(result.Count(g => g.GroupID == item.GroupID) == 0)
								{
									result.Add(new Group()
									{
										GroupID = item.GroupID,
										GroupModifyTime = item.GroupModifyTime,
										GroupDescription = item.GroupDescription,
										GroupCategoryID = item.GroupCategoryID,
										GroupAddTime = item.GroupAddTime,
										GroupName = item.GroupName,
										GroupDispName = item.GroupDispName,
										GroupShortName = item.GroupShortName,
										TheGroupCategory = new GroupCategory()
										{
											GroupCategoryID = item.GroupCategoryID,
											GroupCategoryName = item.GroupCategoryName
										}
									});
								}
							}

							foreach(var group in result)
							{
								var kpisInTheGroup = lstKPIs.Where(kpi => kpi.GroupID == group.GroupID);

								if(kpisInTheGroup != null)
								{
									group.ListKPIs = new List<KPI>();

									foreach(var item in kpisInTheGroup)
									{
										var kpiInTheGroup = new KPI()
										{
											KPIID = item.KPIID,
											TheYear = item.TheYear,
											PreState = item.PreState,
											CurrentState = item.CurrentState,
											ActionPlans = item.ActionPlans,
											RLTel = item.RLTel,
											ResponsibleLeadership = item.ResponsibleLeadership,
											RPTel = item.RPTel,
											ResponsablePerson = item.ResponsablePerson,
											ResponseDepartment = item.ResponseDepartment,
											FinishTime = item.FinishTime,
											GroupID = item.GroupID,
											KPIAddTime = item.KPIAddTime,
											KPIContent = item.KPIContent,
											KPIModifyTime = item.KPIModifyTime,
											StartTime = item.StartTime
										};

										group.ListKPIs.Add(kpiInTheGroup);
									}
								}
							}
						}
					}
				}
				catch
				{
					throw;
				}
			}

			return result;
		}

		public List<Group> GetShowOnHomeGroupWithKPIs()
		{
			List<Group> result = null;

			try
			{
				var groups = from g in _KPIDbContext.Group
										 join gc in _KPIDbContext.GroupCategory on g.GroupCategoryID equals gc.GroupCategoryID into ggc
										 from g_gc in ggc.DefaultIfEmpty()
										 where g.ShowOnHome == 1
										 orderby g.DisplayOrder
										 select new
										 {
											 GroupCategoryID = g_gc.GroupCategoryID,
											 GroupCategoryName = g_gc.GroupCategoryName,
											 //GroupCategoryDescription = g_gc.GroupCategoryDescription,
											 //GroupCategoryAddTime = g_gc.GroupCategoryAddTime,
											 //GroupCategoryModifyTime = g_gc.GroupCategoryModifyTime,
											 DisplayOrder = g.DisplayOrder,
											 GroupAddTime = g.GroupAddTime,
											 GroupDescription = g.GroupDescription,
											 GroupDispName = g.GroupDispName,
											 GroupID = g.GroupID,
											 GroupModifyTime = g.GroupModifyTime,
											 GroupName = g.GroupName,
											 GroupShortName = g.GroupShortName,
											 ShowOnHome = g.ShowOnHome
										 };

				if(groups != null)
				{
					var lstGroups = groups.ToList();

					if(lstGroups != null && lstGroups.Count > 0)
					{
						result = new List<Group>();

						var y = DateTime.Now.Year;

						foreach(var item in lstGroups)
						{
							var theGroup = new Group()
							{
								GroupID = item.GroupID,
								GroupModifyTime = item.GroupModifyTime,
								GroupDescription = item.GroupDescription,
								GroupCategoryID = item.GroupCategoryID,
								GroupAddTime = item.GroupAddTime,
								GroupName = item.GroupName,
								GroupDispName = item.GroupDispName,
								GroupShortName = item.GroupShortName,
								TheGroupCategory = new GroupCategory()
								{
									GroupCategoryID = item.GroupCategoryID,
									GroupCategoryName = item.GroupCategoryName
								}
							};

							var kpis = _KPIDbContext.KPI.Where(kpi => kpi.GroupID == theGroup.GroupID && kpi.TheYear == y && kpi.CurrentState != KPIStates.Delete);

							if(kpis != null)
							{
								theGroup.ListKPIs = kpis.ToList();
							}

							result.Add(theGroup);
						}
					}
				}

				//var kpis = from kpi in _KPIDbContext.KPI
				//           join g in _KPIDbContext.Group on kpi.GroupID equals g.GroupID into kpi2
				//           from kpig in kpi2.DefaultIfEmpty()
				//           join gc in _KPIDbContext.GroupCategory on kpig.GroupCategoryID equals gc.GroupCategoryID into g2
				//           from ggc in g2.DefaultIfEmpty()
				//           where kpi.TheYear == DateTime.Now.Year && kpig.ShowOnHome == 1
				//           orderby kpig.DisplayOrder
				//           select new
				//           {
				//             GroupCategoryName = ggc.GroupCategoryName,
				//             GroupName = kpig.GroupName,
				//             GroupID = kpig.GroupID,
				//             GroupAddTime = kpig.GroupAddTime,
				//             GroupCategoryID = kpig.GroupCategoryID,
				//             GroupDescription = kpig.GroupDescription,
				//             GroupModifyTime = kpig.GroupModifyTime,
				//             GroupDispName = kpig.GroupDispName,
				//             GroupShortName = kpig.GroupShortName,
				//             KPIID = kpi.KPIID,
				//             KPIContent = kpi.KPIContent,
				//             KPIAddTime = kpi.KPIAddTime,
				//             KPIModifyTime = kpi.KPIModifyTime,
				//             StartTime = kpi.StartTime,
				//             FinishTime = kpi.FinishTime,
				//             ResponseDepartment = kpi.ResponseDepartment,
				//             ResponsablePerson = kpi.ResponsablePerson,
				//             RPTel = kpi.RPTel,
				//             ResponsibleLeadership = kpi.ResponsibleLeadership,
				//             RLTel = kpi.RLTel,
				//             ActionPlans = kpi.ActionPlans,
				//             CurrentState = kpi.CurrentState,
				//             PreState = kpi.PreState,
				//             TheYear = kpi.TheYear
				//           };

				//if(kpis != null)
				//{
				//  var lstKPIs = kpis.ToList();

				//  if(lstKPIs != null && lstKPIs.Count > 0)
				//  {
				//    result = new List<Group>();

				//    foreach(var item in lstKPIs)
				//    {
				//      if(result.Count(g => g.GroupID == item.GroupID) == 0)
				//      {
				//        result.Add(new Group()
				//        {
				//          GroupID = item.GroupID,
				//          GroupModifyTime = item.GroupModifyTime,
				//          GroupDescription = item.GroupDescription,
				//          GroupCategoryID = item.GroupCategoryID,
				//          GroupAddTime = item.GroupAddTime,
				//          GroupName = item.GroupName,
				//          GroupDispName = item.GroupDispName,
				//          GroupShortName = item.GroupShortName,
				//          TheGroupCategory = new GroupCategory()
				//          {
				//            GroupCategoryID = item.GroupCategoryID,
				//            GroupCategoryName = item.GroupCategoryName
				//          }
				//        });
				//      }
				//    }

				//    foreach(var group in result)
				//    {
				//      var kpisInTheGroup = lstKPIs.Where(kpi => kpi.GroupID == group.GroupID);

				//      if(kpisInTheGroup != null)
				//      {
				//        group.ListKPIs = new List<KPI>();

				//        foreach(var item in kpisInTheGroup)
				//        {
				//          var kpiInTheGroup = new KPI()
				//          {
				//            KPIID = item.KPIID,
				//            TheYear = item.TheYear,
				//            PreState = item.PreState,
				//            CurrentState = item.CurrentState,
				//            ActionPlans = item.ActionPlans,
				//            RLTel = item.RLTel,
				//            ResponsibleLeadership = item.ResponsibleLeadership,
				//            RPTel = item.RPTel,
				//            ResponsablePerson = item.ResponsablePerson,
				//            ResponseDepartment = item.ResponseDepartment,
				//            FinishTime = item.FinishTime,
				//            GroupID = item.GroupID,
				//            KPIAddTime = item.KPIAddTime,
				//            KPIContent = item.KPIContent,
				//            KPIModifyTime = item.KPIModifyTime,
				//            StartTime = item.StartTime
				//          };

				//          group.ListKPIs.Add(kpiInTheGroup);
				//        }
				//      }
				//    }
				//  }
				//}
			}
			catch
			{
				throw;
			}

			return result;
		}

		/// <summary>
		/// 根据单位类别取单位及KPI和进度
		/// </summary>
		/// <param name="groupCategoryID">单位类别ID</param>
		/// <returns></returns>
		public List<Group> GetGroupWithKPIAndProgressByGroupCategory(int groupCategoryID)
		{
			List<Group> result = null;

			try
			{
				if(groupCategoryID > 0)
				{
					var data = from k in _KPIDbContext.KPI
										 join kp in _KPIDbContext.KPIProgress on k.KPIID equals kp.KPIID into t1
										 from kkp in t1.DefaultIfEmpty()
										 join g in _KPIDbContext.Group on k.GroupID equals g.GroupID into t2
										 from kg in t2.DefaultIfEmpty()
										 where k.TheYear == DateTime.Now.Year && k.CurrentState != KPIStates.Delete && kg.GroupCategoryID == groupCategoryID
										 orderby kg.DisplayOrder
										 select new
										 {
											 KPIID = k.KPIID,
											 KPIContent = k.KPIContent,
											 Quarter1Progress = kkp.Quarter1Progress,
											 Quarter2Progress = kkp.Quarter2Progress,
											 Quarter3Progress = kkp.Quarter3Progress,
											 Quarter4Progress = kkp.Quarter4Progress,
											 GroupID = kg.GroupID,
											 GroupName = kg.GroupName,
											 GroupShortName = kg.GroupShortName,
											 GroupDispName = kg.GroupDispName,
											 GroupCategoryID = kg.GroupCategoryID
										 };

					if(data != null)
					{
						var lstData = data.ToList();

						if(lstData != null && lstData.Count > 0)
						{
							result = new List<Group>();

							foreach(var item in lstData)
							{
								if(result.Count(d => d.GroupID == item.GroupID) == 0)
								{
									result.Add(new Group()
									{
										GroupID = item.GroupID,
										GroupDispName = item.GroupDispName,
										GroupName = item.GroupName,
										GroupShortName = item.GroupShortName
									});
								}
							}

							foreach(var group in result)
							{
								var kpis = lstData.Where(d => d.GroupID == group.GroupID);

								if(kpis != null)
								{
									group.ListKPIs = new List<KPI>();

									foreach(var kpiItem in kpis)
									{
										var kpi = new KPI()
										{
											KPIID = kpiItem.KPIID,
											KPIContent = kpiItem.KPIContent,
											GroupID = kpiItem.GroupID
										};

										kpi.TheKPIProgress = new KPIProgress()
										{
											KPIID = kpiItem.KPIID,
											Quarter1Progress = kpiItem.Quarter1Progress,
											Quarter2Progress = kpiItem.Quarter2Progress,
											Quarter3Progress = kpiItem.Quarter3Progress,
											Quarter4Progress = kpiItem.Quarter4Progress
										};

										group.ListKPIs.Add(kpi);
									}
								}
							}
						}
					}
				}
				else
				{
					var data = from k in _KPIDbContext.KPI
										 join kp in _KPIDbContext.KPIProgress on k.KPIID equals kp.KPIID into t1
										 from kkp in t1.DefaultIfEmpty()
										 join g in _KPIDbContext.Group on k.GroupID equals g.GroupID into t2
										 from kg in t2.DefaultIfEmpty()
										 where k.TheYear == DateTime.Now.Year && k.CurrentState != KPIStates.Delete
										 orderby kg.DisplayOrder
										 select new
										 {
											 KPIID = k.KPIID,
											 KPIContent = k.KPIContent,
											 Quarter1Progress = kkp.Quarter1Progress,
											 Quarter2Progress = kkp.Quarter2Progress,
											 Quarter3Progress = kkp.Quarter3Progress,
											 Quarter4Progress = kkp.Quarter4Progress,
											 GroupID = kg.GroupID,
											 GroupName = kg.GroupName,
											 GroupShortName = kg.GroupShortName,
											 GroupDispName = kg.GroupDispName,
											 GroupCategoryID = kg.GroupCategoryID
										 };

					if(data != null)
					{
						var lstData = data.ToList();

						if(lstData != null && lstData.Count > 0)
						{
							result = new List<Group>();

							foreach(var item in lstData)
							{
								if(result.Count(d => d.GroupID == item.GroupID) == 0)
								{
									result.Add(new Group()
									{
										GroupID = item.GroupID,
										GroupDispName = item.GroupDispName,
										GroupName = item.GroupName,
										GroupShortName = item.GroupShortName
									});
								}
							}

							foreach(var group in result)
							{
								var kpis = lstData.Where(d => d.GroupID == group.GroupID);

								if(kpis != null)
								{
									group.ListKPIs = new List<KPI>();

									foreach(var kpiItem in kpis)
									{
										var kpi = new KPI()
										{
											KPIID = kpiItem.KPIID,
											KPIContent = kpiItem.KPIContent,
											GroupID = kpiItem.GroupID
										};

										kpi.TheKPIProgress = new KPIProgress()
										{
											KPIID = kpiItem.KPIID,
											Quarter1Progress = kpiItem.Quarter1Progress,
											Quarter2Progress = kpiItem.Quarter2Progress,
											Quarter3Progress = kpiItem.Quarter3Progress,
											Quarter4Progress = kpiItem.Quarter4Progress
										};

										group.ListKPIs.Add(kpi);
									}
								}
							}
						}
					}
				}
			}
			catch
			{
				throw;
			}

			return result;
		}

		#endregion
	}
}
